package com.test.qpbgboot.base.config;

import com.test.qpbgboot.base.constants.RabbitMQConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Configuration
public class RabbitMQConfig {

    @Bean
    public DirectExchange waitingPayExchange() {
        return new DirectExchange(
                RabbitMQConstants.ORDER_WAITING_PAY_EXCHANGE,
                RabbitMQConstants.DURABLE, false);
    }

    @Bean
    public Queue waitingPayQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange",
                RabbitMQConstants.ORDER_WAITING_PAY_DEAD_EXCHANGE);
        args.put("x-dead-letter-routing-key",
                RabbitMQConstants.ORDER_WAITING_PAY_DEAD_ROUTING_KEY);
        args.put("x-message-ttl",
                RabbitMQConstants.WAITING_PAY_TTL);
        return new Queue(
                RabbitMQConstants.ORDER_WAITING_PAY_QUEUE,
                RabbitMQConstants.DURABLE, false, false, args);
    }

    @Bean
    public Binding waitingPayBinding() {
        return BindingBuilder
                .bind(waitingPayQueue())
                .to(waitingPayExchange())
                .with(RabbitMQConstants.ORDER_WAITING_PAY_ROUTING_KEY);
    }

    @Bean
    public DirectExchange waitingPayDeadExchange() {
        return new DirectExchange(
                RabbitMQConstants.ORDER_WAITING_PAY_DEAD_EXCHANGE,
                RabbitMQConstants.DURABLE, false);
    }

    @Bean
    public Queue waitingPayDeadQueue() {
        return new Queue(
                RabbitMQConstants.ORDER_WAITING_PAY_DEAD_QUEUE,
                RabbitMQConstants.DURABLE, false, false);
    }

    @Bean
    public Binding waitingPayDeadBinding() {
        return BindingBuilder
                .bind(waitingPayDeadQueue())
                .to(waitingPayDeadExchange())
                .with(RabbitMQConstants.ORDER_WAITING_PAY_DEAD_ROUTING_KEY);
    }

}
